package pe.com.empresa.utiles;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;


public class ActionHelper extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	protected SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); 
	
	public HttpSession getSessionContext(boolean flag){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(flag);
		return session;
	}
	
	public HttpSession getSessionContext(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		return session;
	}
	
	public SqlSessionFactory getSqlSessionFactory(){
		return SessionFactory.getSqlSessionFactory();
	}
	
	public ActionHelper() {
		
	}
	
	@SuppressWarnings("rawtypes")
	public String toListJson(List list){
		DataTablesUtil dt = new DataTablesUtil();
		if(list == null){
			System.out.println("es nulo");
			dt.setAaData(new ArrayList());
		}else{
			System.out.println("no es nulo");
			System.out.println(list.size());
			dt.setAaData(list);
		}
		Gson json = new Gson();
		return json.toJson(dt);
	}
	
	public String toJson(Object obj){
		Gson json = new Gson();
		return json.toJson(obj);
	}
	
	public Date dateFormat(String dateString) throws ParseException{
		Date dateReturn;
		if(dateString.equals("")){
			dateReturn=null;
		}else{
			Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateString);
			String dateString2 = new SimpleDateFormat("yyyy-MM-dd").format(date);
			dateReturn = simpleFormat.parse(dateString2);
		}

		return dateReturn;
	}
	
	public boolean buscarArticuloEnGrilla(List<GrillaArticulos> grilla, String codigo){
		for(GrillaArticulos articulo : grilla){
			if(articulo.getCodigoArticulo().equals(codigo)){
				return true;
			}
		}
		return false;
	}
	

	
	public boolean validarInicioSesion(){
		
		
		System.out.println("------------------------------------------");
		System.out.println(this.getSessionContext().getAttribute(Constantes.SESION_USUARIO));
		System.out.println(this.getSessionContext().getAttribute(Constantes.SESION_PERSONA));
		System.out.println(this.getSessionContext().getAttribute(Constantes.SESION_ROL));
		System.out.println(this.getSessionContext().getAttribute(Constantes.SESION_ROLES));
		System.out.println(this.getSessionContext().getAttribute(Constantes.SESION_PERMISOS));
		System.out.println("------------------------------------------");
		
		if( 
				this.getSessionContext().getAttribute(Constantes.SESION_USUARIO) != null &&
				this.getSessionContext().getAttribute(Constantes.SESION_PERSONA) != null &&
				this.getSessionContext().getAttribute(Constantes.SESION_ROL) != null &&
				this.getSessionContext().getAttribute(Constantes.SESION_ROLES) != null &&
				this.getSessionContext().getAttribute(Constantes.SESION_PERMISOS) != null
		  )
		{
			return true;
		}
		
		return false;
	}
		
}
